package com.jy.rhin.infra.repo.notfication.participant;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.jy.common.UIDWorker;
import com.jy.rhin.domain.notification.participant.ParticipantDao;
import com.jy.rhin.domain.notification.participant.entity.ParticipantDO;
import com.jy.rhin.infra.repo.notfication.participant.mapper.ParticipantMapper;
import com.jy.rhin.infra.repo.notfication.participant.po.ParticipantPO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author dhl
 * @date 2022/6/29
 * @Description
 */
@Service
public class ParticipantDaoImpl implements ParticipantDao {

    @Resource
    UIDWorker uidWorker;
    @Resource
    private ParticipantMapper participantMapper;


    @Override
    public ParticipantDO queryById(Long id) {
        ParticipantPO po = participantMapper.selectById(id);
        return ObjectUtils.isNull(po) ? null : po.toDO();
    }

    @Override
    public ParticipantDO queryByCode(String code) {
        QueryWrapper<ParticipantPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ParticipantPO::getParticipantCode, code);
        ParticipantPO po = participantMapper.selectOne(queryWrapper);
        return ObjectUtils.isNull(po) ? null : po.toDO();
    }

    @Override
    public Long save(ParticipantDO participant) {
        if(ObjectUtils.isNull(participant.getId())){
            participant.setId(uidWorker.nextId());
            participantMapper.insert(new ParticipantPO(participant));
        }else{
            participantMapper.updateById(new ParticipantPO(participant));
        }
        return participant.getId();
    }

}
